$(document).ready(function() {

    //Check if url hash value exists (for bookmark)
    $.history.init(pageload);

    //highlight the selected link
    $('a[href=' + document.location.hash + ']').addClass('selected');

    //Seearch for link with REL set to ajax
    $('a[rel=ajax]').click(function() {

        //grab the full url
        var hash = this.href;

        //remove the # value
        hash = hash.replace(/^.*#/, '');

        //for back button
        $.history.load(hash);

        //clear the selected class and add the class class to the selected link
        $('a[rel=ajax]').removeClass('selected');
        $(this).addClass('selected');

        //hide the content and show the progress bar
        $('#content-load').hide();
        $('#container').remove();

        $('#loading-server').show();

        //run the ajax
        getPage();

        //cancel the anchor tag behaviour
        return false;
    });
});


function pageload(hash) {
    //if hash value exists, run the ajax
    if (hash)
        getPage();
}

function getPage() {
    Pace.restart();

    paceOptions = {
        ajax: true, // disabled
        document: true, // disabled
        eventLag: true, // disabled
        elements: {
            selectors: ['.my-page']
        }
    };
    //generate the parameter for the php script
    var page = document.location.hash;
    var data = 'accion=list';
    var url = '';
    switch (page) {
        case '#persona' :
            url = 'fuentes/controller/Persona.php';
            break;
        case '#persona-admin' :
            url = './web/view/personaAdmin/PersonasView.php';
            break;
        case '#movimiento-time' :
            url = 'fuentes/controller/Movimiento.php';
            break;
        case '#bleu-evento' :
            url = 'fuentes/controller/BleuEvento.php';
            break;
        case '#sucursal' :
            url = 'fuentes/controller/Sucursal.php';
            break;
        case '#sector' :
            url = 'fuentes/controller/Sector.php';
            break;
        case '#sorteo' :
            url = 'fuentes/controller/Sorteo.php';
            break;
        case '#cupon' :
            url = 'fuentes/controller/Cupon.php';
            break;
        case '#encuesta' :
            url = 'fuentes/controller/Encuesta.php';
            break;
        case '#respuesta' :
            url = 'fuentes/controller/Respuesta.php';
            break;
        case '#poker-torneo' :
            url = 'fuentes/controller/PokerTorneo.php';
            break;
        case '#moneda' :
            url = 'fuentes/controller/Moneda.php';
            break;
        case '#bleu-specifics' :
            url = 'fuentes/controller/BleuSpecific.php';
            break;
        case '#bleu-ranking-visitas' :
            url = 'fuentes/controller/BleuRankingVisitas.php';
            break;
        case '#pais' :
            url = 'fuentes/controller/Pais.php';
            break;
        case '#ciudad' :
            url = 'fuentes/controller/Ciudad.php';
            break;
        case '#participante' :
            url = 'fuentes/controller/PokerSpecific.php';
            break;
        case '#poker-movimiento' :
            url = 'fuentes/controller/PokerMovimiento.php';
            break; 
        case '#poker-estructura' :
            url = 'fuentes/controller/PokerEstructura.php';
            break; 
       case '#poker-evento' :
            url = 'fuentes/controller/PokerEvento.php';
            break;  

        default :
            url = './404.php';
    }

    $.ajax({
        url: url,
        type: "POST",
        data: data,
        cache: false,
        success: function(html) {

            //hide the progress bar
            $('#loading-server').hide();

            //add the content retrieved from ajax and put it in the #content div
            $('#content-load').html(html);

            //display the body with fadeIn transition
            $('#content-load').fadeIn('slow');
        }
    });
}

//limpiar 
function clear(form) {
    var parent = $(form).parent('.input-with-icon');
    parent.removeClass('error-control').addClass();
    parent.removeClass('success-control').addClass();
}

function numeroEntero(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;
    return true;
}

function numeroDecimal(field, evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode == 46)
               {
                   var inputValue = field.value;
                   if (inputValue.indexOf('.') < 1) {
                       return true;
                   }
                   return false;
               }
    if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 46)
        return false;

    return true;
}